home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / resize.zip / RESIZE.C < prev    next >
C/C++ Source or Header  |  1991-05-15  |  4KB  |  125 lines

  1. /*--------------------------------------------------------------------------*
  2.  *                                                                          *
  3.  *  RESIZE:     Adjust the size of a file to be equal to the number of      *
  4.  *              characters found before Control-Z.                          *
  5.  *                                                                          *
  6.  *--------------------------------------------------------------------------*
  7.  *                                                                          *
  8.  *  NOTES:      Compiled using MSC 6.0                                      *
  9.  *                                                                          *
  10.  *                  CL /AT resize.c                                         *
  11.  *                                                                          *
  12.  *--------------------------------------------------------------------------*/
  13.  
  14. #include <dos.h>
  15. #include <io.h>
  16. #include <stdio.h>
  17.  
  18.  
  19. static char         buffer[32000];
  20.  
  21. void            main(int argc, char **argv)
  22. {
  23.     int             i, fh, c, errors;
  24.     long            fs, rl, sl, ol;
  25.     unsigned        fd, ft;
  26.     char          **fn, *p;
  27.     FILE           *fp;
  28.  
  29.  
  30.     if (argc < 2)
  31.     {
  32.         printf("RESIZE V1.00 - Resize file to retain data before Control-Z.\n"
  33.                "Copyright 1991, Gee Wiz Software.  All rights reserved.\n\n"
  34.                "Usage: resize file-1 [file-2 ... file-n]\n\n");
  35.         exit(0);
  36.     }
  37.  
  38.     for (errors = 0, i = 1, fn = argv + 1, fp = NULL; i < argc; i++, fn++)
  39.     {
  40.         if (fp != NULL)
  41.         {
  42.             clearerr(fp);
  43.             fclose(fp);
  44.         }
  45.  
  46.         if ((fp = fopen(*fn, "r+b")) == NULL)
  47.         {
  48.             errors++;
  49.             printf("* OPEN failed on file %s.\n", *fn);
  50.             continue;
  51.         }
  52.  
  53.         fh = fileno(fp);
  54.  
  55.         if ((ol = filelength(fh)) == -1)
  56.         {
  57.             errors++;
  58.             printf("* INFO failed on file %s.\n", *fn);
  59.             continue;
  60.         }
  61.  
  62.         for (fs = 0;;)
  63.         {
  64.             rl = fread(buffer, 1, sizeof(buffer), fp);
  65.  
  66.             for (sl = 0, p = buffer; sl < rl && *p != 0x1a; sl++, p++);
  67.  
  68.             fs += sl;
  69.  
  70.             if (sizeof(buffer) > rl)
  71.             {
  72.                 if (feof(fp))
  73.                     break;
  74.  
  75.                 if (ferror(fp))
  76.                 {
  77.                     fs = -1;
  78.                     errors++;
  79.                     printf("* READ failed on file %s.\n", *fn);
  80.                     break;
  81.                 }
  82.             }
  83.         }
  84.  
  85.  
  86.         if (fs != -1)
  87.         {
  88.             do
  89.             {
  90.                 if (fs >= ol)
  91.                 {
  92.                     printf("- File %s left intact at %ld bytes.\n", *fn, ol);
  93.                     break;
  94.                 }
  95.  
  96.                 _dos_getftime(fh, &fd, &ft);
  97.  
  98.                 if (chsize(fh, fs) == -1)
  99.                 {
  100.                     errors++;
  101.                     printf("* SIZE failed on file %s.\n", *fn);
  102.                     break;
  103.                 }
  104.  
  105.                 _dos_setftime(fh, fd, ft);
  106.  
  107.                 printf("+ File %s resized to %ld bytes.\n", *fn, fs);
  108.  
  109.             }   while(0);
  110.         }
  111.     }
  112.  
  113.  
  114.     if (fp != NULL)
  115.     {
  116.         clearerr(fp);
  117.         fclose(fp);
  118.     }
  119.  
  120.     if (errors)
  121.         printf("%d %s occurred.\n", errors, (errors == 1) ? "error" : "errors");
  122.  
  123.     exit(errors);
  124. }
  125.